package com.zxy.salenet.web.service.imple;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.mapper.SalenetAdminsMapper;
import com.zxy.salenet.web.pojo.SalenetAdmins;
import com.zxy.salenet.web.service.AdminManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author: zxy
 * @version: 1.0
 * @description:
 */
 /**
 * AdminManageServiceImpl
 */
@Service
public class AdminManageServiceImpl implements AdminManageService {
    @Autowired
    private SalenetAdminsMapper adminsMapper;

    /**
     * 获取所有的管理员信息|按姓名模糊查找对应信息|按具体姓名查询信息
     *
     * @param adminRealName 管理员姓名
     * @param pageNum       当前页
     * @param pageSize      总页数
     * @return
     */
    @Override
    public PageInfo<SalenetAdmins> queryAllAdminsMessage(String adminRealName, Integer pageNum, Integer pageSize) {
        adminRealName = "%"+adminRealName+"%";
        PageHelper.startPage(pageNum,pageSize);
        List<SalenetAdmins> queryAllAdmins = adminsMapper.selectAllAdmin(adminRealName);
        PageInfo<SalenetAdmins> pageInfo = new PageInfo<>(queryAllAdmins);
        return pageInfo;
    }

    /**
     * 添加一个管理员信息
     * @param admins
     * @return
     */
    @Override
    public Integer addAdmin(SalenetAdmins admins) {
        //获取登陆用户名
        String adminName = admins.getAdminsName();
        //获取真实姓名
        String adminRealName = admins.getAdminsRealname();
        //先查在添加
        Integer repResult = adminsMapper.selectCountByAdmins(adminName,adminRealName);
        if(repResult !=0 ){
            return 2;
        }
        //添加管理员
        admins.setAdminsPasswd(DigestUtils.md5DigestAsHex(("zxy"+ admins.getAdminsPasswd()).getBytes()));
        Integer insertResult = adminsMapper.insertAdmins(admins);
        return insertResult;
    }

    /**
     * 修改普通管理员信息
     * @param admins
     * @return
     */
    @Override
    public Integer updateAdmin(SalenetAdmins admins) {
        Integer id = adminsMapper.selectIdByName(admins.getAdminsRealname());
        admins.setId(id);
        int updateResult = adminsMapper.updateByPrimaryKeySelective(admins);
        return updateResult;
    }

    @Override
    public Integer deleteAdmin(Integer id) {
        Integer deleteResult = adminsMapper.deleteAdminById(id);
        return deleteResult;
    }
}
